<style lang='scss'>
    .addlabel-title{
        display: block;
        font-size: 13px;
        background-color: #fff;
        padding: 12px 16px;
        border-bottom: 1px solid #ddd;
        margin: 0 -10px 6px -12px;
    }
</style>
<template>
	<div>
        <div class="addlabel-title" v-if="$route.query.id">
            {{'承运商服务黑名单 / 编辑'}}
        </div>
		<compWrapper title="基本信息" style="padding: 10px">
			<compFormQuery v-if=" type === 'query'" class="margin-top-15" :formdata="getBase" :value="formData"></compFormQuery>
            <compFormEdit v-else ref="formbase" class="margin-top-15" :formdata="getBase" v-model="formData"></compFormEdit>
		</compWrapper>
        <compWrapper title="附加属性" style="padding: 10px">
            <div style="margin-top: 15px; padding: 10px 0 10px 50px">
                <Checkbox v-model="forbidAll">
                    <Tooltip content="客户无法选择对应的承运商服务和使用测算服务" placement="top">
                        <span style="">屏蔽承运商服务&测算</span>
                    </Tooltip>
                </Checkbox>
                <Checkbox v-model="isCalculate">
                    <Tooltip content="只屏蔽测算服务，客户可正常选择该承运商服务" placement="top">
                        <span style="">屏蔽测算</span>
                    </Tooltip>
                </Checkbox>
                <Checkbox v-model="isBill">
                    <Tooltip content="客户组织未配置费率正常使用，不报错" placement="top">
                        <span style="">承运商服务不强制计费</span>
                    </Tooltip>
                </Checkbox>
            </div>
		</compWrapper>
        <compWrapper title="操作历史" v-if=" type === 'query' ">
			<compLog class="margin-15" :data="opLogList" :columns="logcolumn"></compLog>
		</compWrapper>
		<div v-if="type != 'query'" class="text-right margin-top-10">
			<Button type="primary" @click="submit">保存</Button>
			<Button v-if="type != 'add'" class="margin-left-10" @click="onCancel">返回</Button>
		</div>
		<Spin v-if="loading" fix></Spin>
	</div>
</template>

<script>
import { HttpCarrierServiceBlackDetail, HttpCarrierServiceBlackSave, HttpGetWarehouseByOrg, HttpGetProductByOrgLabel, HttpGetProductServiceByOrg } from '@/service/api/views';
import { mapGetters } from "vuex";
export default {
	props:['querytype','activeid'],
	data() {
		return {
            type: this.querytype || this.$route.params.type,
            id: this.activeid || this.$route.query.id,
            loading: false,
            formData: {
                status: true,
            },
            warehouseList1: [],
            serviceList: [],
            productList: [],
            isCalculate: false,
            isBill: false,
            forbidAll: false,
		};
	},
	created() {
        this.findService();
        this.getDetail();
	},
	computed: {
         ...mapGetters('dictionary',['getBillcusorgList']),
		getBase() {
			return [
                {
                    label: '客户组织',
                    key: 'organizationId',
                    tag: 'Select',
                    options: this.getBillcusorgList,
                    props:{
                        filterable: true,
                    },
                    on:{
                        'on-change':(val,val1)=>{
                            if(val1){
                                this.findWarehouse(val1);
                                this.findProduct(val1);
                                this.formData.orgWarehouseId = '';
                                this.formData.carrierGroupId = '';
                            }
                        }
                    }
                },
                {
                    label: '仓库',
                    key: 'orgWarehouseId',
                    tag: 'Select',
                    options: this.warehouseList1,
                    props:{
                        filterable: true,
                    },
                    required: false,
                },
                {
                    label: '物流产品',
                    key: 'carrierGroupId',
                    tag: 'Select',
                    options: this.productList,
                    props:{
                        filterable: true,
                    },
                },
                {
                    label: '承运商服务',
                    key: 'carrierServiceIds',
                    tag: 'Select',
                    options: this.serviceList,
                    props:{
                        filterable: true,
                        multiple: this.type == 'add',
                    },
                },
                {
                    label: "状态",
                    key: 'status',
                    tag: 'Switch',
                }
			];
		},
        logcolumn(){
            return [
                {
                    title: this.$ct('global.common.operate_time',{defaultValue:'操作时间'}),
                    key: 'opAt',
                },
                {
                    title: this.$ct('global.common.operator',{defaultValue:'操作人'}),
                    key: 'opUserName',
                },
                {
                    title: this.$ct('global.common.operate_type',{defaultValue:'操作类型'}),
                    render:(h,{row})=>{
                        return this.findRenderLabel(this.operaTypelist, row.operateType);
                    }
                },
                {
                    title: this.$ct('erp.product.currency', { defaultValue: '说明' }),
                    key: 'opRemark',
                },
            ]
        }
	},
	methods: {
        async findWarehouse(org){
            this.loading = true;
            await HttpGetWarehouseByOrg({customerOrgId: org}).then(res=>{
                this.loading = false
                this.warehouseList1 = res.data.map(item=>{
                    return {value: item.id, label: `${item.code}(${item.title})`, attach: item}
                })
            },err=>{
                this.loading = false
                this.$Message.error({
                    content: err.message,
                    duration: 30,
                    closable: true,
                });
            })
        },
        async findProduct(org){
            this.loading = true;
            await HttpGetProductByOrgLabel({customerId: org}).then(res=>{
                this.loading = false
                this.productList = res.data.map(item=>{
                    return {value: item.id, label: item.carrierGroupCode, attach: item}
                })
            },err=>{
                this.loading = false
                this.$Message.error({
                    content: err.message,
                    duration: 30,
                    closable: true,
                });
            })
        },
        async findService(org){
            await HttpGetProductServiceByOrg({'search[EQ_active]': 1}).then(res=>{
                this.serviceList = res.data.map(item=>{
                    return {value: item.id, label: item.carrierServiceCode, attach: item}
                })
            },err=>{
                this.$Message.error({
                    content: err.message,
                    duration: 30,
                    closable: true,
                });
            })
        },
        onCancel() {
            if(this.type == 'add'){
                this.$emit('on-update');
                this.$emit('on-close');
            }else{
                this.$router.push({
                    name: "tmsCarrierBlackList"
                })
            }
		},
		async getDetail() {
			if (this.type === "add") return;
			this.loading = true;
            let arr = {};
			await HttpCarrierServiceBlackDetail({
				id: this.id,
			}).then(res => {
                // this.findWarehouse(res.data.organizationId);
                // this.findProduct(res.data.organizationId);
                // this.formData = res.data;
                arr = res.data;
                // this.formData.carrierServiceIds = [];
                // this.formData.carrierServiceIds.push(res.data.carrierServiceId);
                // this.formData.status = res.data.active == 1 ? true : false;
                this.loading = false;
			}, err => {
				this.loading = false;
				this.$Message.error(err.message);
			});
            await this.findWarehouse(arr.organizationId);
            await this.findProduct(arr.organizationId);
            this.formData = arr;
            this.formData.carrierServiceIds = [];
            this.formData.carrierServiceIds.push(arr.carrierServiceId);
            this.formData.status = arr.active == 1 ? true : false;
            this.isCalculate = arr.isCalculate == 1 ? true: false;
            this.isBill = arr.isBill == 1 ? true : false;
            this.forbidAll = arr.forbidAll == 1 ? true : false;
		},
		async submit() {
			const valid = await this.$refs.formbase.validate();
			if (!valid) return;
            if(!this.isCalculate && !this.isBill && !this.forbidAll){
                return this.$Message.error({
                    content: '附加属性必填',
                    duration: 30,
                    closable: true
                });
            }
            const active = this.formData.status ? 1: 0;
            let uploadData = this.type == 'add' ? {
                hostOrgId: global.SYSID,
                organizationId: this.formData.organizationId,
                orgWarehouseId: this.formData.orgWarehouseId,
                carrierGroupId: this.formData.carrierGroupId,
                carrierServiceIds: this.formData.carrierServiceIds,
                active,
                isCalculate: this.isCalculate ? 1: 0,
                isBill: this.isBill ? 1 : 0,
                forbidAll: this.forbidAll ? 1 : 0,
            } : {
                id: this.id,
                hostOrgId: global.SYSID,
                organizationId: this.formData.organizationId,
                orgWarehouseId: this.formData.orgWarehouseId,
                carrierGroupId: this.formData.carrierGroupId,
                carrierServiceId: this.formData.carrierServiceIds,
                active,
                isCalculate: this.isCalculate ? 1: 0,
                isBill: this.isBill ? 1 : 0,
                forbidAll: this.forbidAll ? 1 : 0,
            }
			this.loading = true;
			HttpCarrierServiceBlackSave({
                ...uploadData
			}).then(res => {
				this.loading = false;
                this.$Message.success(res.message)
				this.onCancel();
			}, err => {
				this.loading = false;
				this.$Message.error(err.message);
			})
		},
	}
};
</script>
